package com.itheima.controller;

import com.itheima.common.constant.MessageConstant;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.common.utils.AliOssUtil;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal")
@Slf4j
@Api(tags = "套餐管理")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private AliOssUtil aliOssUtil;
    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public Result upload(MultipartFile imgFile) throws IOException {
        log.info("上传文件到阿里云:{}",imgFile);
        //调用阿里云OSS工具类，将上传上来的文件存入阿里云
        String url = null;
        try {
            //原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //截取原始文件名的后缀   dfdfdf.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString() + extension;
            url = aliOssUtil.upload(imgFile.getBytes(),objectName);
            //将上传图片名称存入Redis，基于Redis的Set集合存储
            redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_RESOURCES,url);
        } catch (IOException e) {
            return new Result (true,MessageConstant.PIC_UPLOAD_FAIL,url);
        }
        //将图片上传完成后的url返回，用于浏览器回显展示
        return new Result (true,MessageConstant.PIC_UPLOAD_SUCCESS,url);
    }

    /**
     * 新增套餐
     * @param setmeal
     * @param checkgroupIds
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("新增套餐")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkgroupIds){
        log.info("新增套餐：{},{}",setmeal,checkgroupIds);
        try {
            setmealService.add(setmeal,checkgroupIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_SETMEAL_FAIL);
        }
        return new Result (true, MessageConstant.ADD_SETMEAL_SUCCESS);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    @ApiOperation("套餐分页查询")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){

        return setmealService.findPage(queryPageBean);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @GetMapping("/findById")
    @ApiOperation("根据id查询套餐")
    public Result findById(Integer id){
        Setmeal setmeal = setmealService.findById(id);
        if(setmeal != null){
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        }
        return new Result(false,MessageConstant.QUERY_SETMEAL_FAIL);
    }


    /**
     * 编辑套餐
     * @param setmeal
     * @param checkGroupIds
     * @return
     */
    @PostMapping("edit")
    @ApiOperation("编辑套餐")
     public Result edit(@RequestBody Setmeal setmeal, Integer[] checkGroupIds){
        log.info("编辑套餐：{},{}",setmeal,checkGroupIds);
        try {
            setmealService.edit(setmeal,checkGroupIds);
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
     }

    /**
     * 删除套餐
     * @param id
     * @return
     */
    public Result deleteById(Integer id){
        try {
            setmealService.deleteById(id);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result (true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }
}
